from fastapi import FastAPI
from fastapi_offline import FastAPIOffline
from fastapi.staticfiles import StaticFiles

from config import get_config
from app.log import register_log
from app.middleware import register_middleware
from app.exception import register_exception
from app.event import register_events

from router import api_router

def create_app() -> FastAPI:
    settings = get_config()

    app = FastAPIOffline(
        debug = settings.DEBUG,
        title = settings.TITLE,
        description = settings.DESCP,
        docs_url = settings.DOC_URL,
        version  = settings.VERSION
    )
    # 事件监听
    register_events(app)
    # 注册捕获全局异常
    register_exception(app)
    # 设置运行日志
    register_log(app)
    # 中间件设置
    register_middleware(app)
    # 挂在静态文件目录
    app.mount('/www', StaticFiles(directory=settings.WEB_DIR), name="www")
    app.mount('/upload', StaticFiles(directory=settings.UPLOAD_DIR), name="upload")
    # 挂载API路由
    app.include_router(api_router)
    
    return app